/**
 * 
 */
package com.toncen.samepms.common;

import java.text.DecimalFormat;
import java.text.FieldPosition;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sky.common.string.StringUtils;
import com.sky.simple.engine.IWorkflowParticipator;

/**
 * @author tch
 * 
 */
public abstract class AbstractDomain implements IDomain {

	/**
	 * Domain的id，默认为为持久化
	 */
	private long id = IDomain.UNSAVED_ID;

	/**
	 * 状态
	 */
	private int state = 1;

	/**
	 * 备注
	 */
	private String remark;

	/**
	 */
	protected Log logger = LogFactory.getLog(this.getClass());

	/**
	 * 参与者
	 */
	private IWorkflowParticipator partner;

	public AbstractDomain() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.domain.IDomain#getId()
	 */
	public long getId() {
		return id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.domain.IDomain#setId(long)
	 */
	public void setId(long id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDomain#getState()
	 */
	public int getState() {
		return state;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDomain#setState(int)
	 */
	public void setState(int state) {
		this.state = state;
	}

	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}

	/**
	 * @param remark
	 *            the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDomain#isSaved()
	 */
	public boolean isSaved() {
		return isSaved(this);
	}

	/**
	 * 判断指定的domain是否已经持久化
	 * 
	 * @param domain
	 * @return 持久化则返回true,没有持久化则返回false
	 */
	public static boolean isSaved(IDomain domain) {
		return domain.getId() != IDomain.UNSAVED_ID && domain.getId() != 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + (int) (getId() ^ (getId() >>> 32));
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final IDomain other = (IDomain) obj;
		if (!other.isSaved()) {
			return false;
		}
		if (getId() != other.getId())
			return false;
		return true;
	}

	/**
	 * 格式化数字
	 * 
	 * @param value
	 * @param patterm
	 * @return
	 */
	public String formate(Object number, String patterm) {
		if (StringUtils.isEmpty(patterm)) {
			patterm = "#0.00";
		}
		DecimalFormat decimalFormat = new DecimalFormat(patterm);
		String result = decimalFormat.format(number, new StringBuffer(), new FieldPosition(0)).toString();
		return result;
	}

	/**
	 * @return
	 */
	public long getActivityId() {
		return getState();
	}

	/**
	 * @param activityId
	 */
	public void setActivityId(long activityId) {
		setState((int) activityId);
	}

	/**
	 * @return
	 */
	public IWorkflowParticipator getParticipator() {
		return partner;
	}

	/**
	 * @param participator
	 */
	public void setParticipator(IWorkflowParticipator participator) {
		this.partner = participator;
	}

	/**
	 * @return
	 */
	public IWorkflowParticipator getPartner() {
		return partner;
	}

	/**
	 * @param partner
	 */
	public void setPartner(IWorkflowParticipator partner) {
		this.partner = partner;
	}
	
	/**
	 * @param domain
	 */
	public IDomain beforeImportSave(IDomain domain){
		
		return domain;
	}
}
